home *** CD-ROM | disk | FTP | other *** search
/ Aminet 44 / Aminet 44 (2001)(GTI - Schatztruhe)[!][Aug 2001].iso / Aminet / comm / misc / Sashi89.lha / Sashi89 / sources / timer.c < prev    next >
C/C++ Source or Header  |  2001-05-05  |  2KB  |  103 lines

  1. #include <exec/types.h>
  2. #include <exec/io.h>
  3. #include <exec/memory.h>
  4. #include <devices/timer.h>
  5.  
  6. #include <proto/exec.h>
  7. #include <proto/alib.h>
  8. #include <proto/dos.h>
  9.  
  10. #include <stdio.h>
  11.  
  12.  
  13. #include "timer.h"
  14.  
  15.  
  16. /*struct  timerequest *timer_create( ULONG unit );
  17. void    timer_delete(struct timerequest *tr );
  18. void    timer_waitfor(struct timerequest *tr, struct timeval *tv );*/
  19.  
  20. /* sleep for 500,000 micro-seconds = 1/2 second */
  21. /*
  22. currentval.tv_secs = 0;
  23. currentval.tv_micro = 500000;
  24. time_delay( ¤tval, UNIT_MICROHZ );
  25. printf( "After 1/2 second delay\n" );
  26. */
  27.  
  28.  
  29.  
  30.  
  31. int timer_create( Timer_Info *ti)
  32. {
  33.   LONG error;
  34.  
  35.   ti->created = 0;
  36.  
  37.   ti->timerport = CreatePort( 0, 0 );
  38.   if ( ti->timerport == NULL )
  39.     return( 1 );
  40.  
  41.   ti->TimerIO = (struct timerequest *)
  42.       CreateExtIO( ti->timerport, sizeof( struct timerequest ) );
  43.  
  44.   if (ti->TimerIO == NULL )
  45.   {
  46.     DeletePort(ti->timerport);   /* Delete message port */
  47.     return( 1 );
  48.   }
  49.  
  50.   error = OpenDevice( TIMERNAME, UNIT_MICROHZ,(struct IORequest *) ti->TimerIO, 0L );
  51.   if (error != 0 )
  52.   {
  53.     timer_delete( ti );
  54.     return( 1 );
  55.   }
  56.  
  57.   ti->tv.tv_secs   = 0;
  58.  
  59.   ti->created = 1;
  60.   return(0);
  61. }
  62.  
  63. void timer_delay(Timer_Info *ti, ULONG microsecs)
  64. {
  65.   ti->tv.tv_secs   = (microsecs/1000000);
  66.   ti->tv.tv_micro  = (microsecs%1000000);
  67.  
  68.   //printf("waiting for %lds %ldµs\n",ti->tv.tv_secs,ti->tv.tv_micro);
  69.  
  70.   ti->TimerIO->tr_node.io_Command = TR_ADDREQUEST; /* add a new timer request */
  71.  
  72.   /* structure assignment */
  73.   ti->TimerIO->tr_time = ti->tv;
  74.  
  75.   /* post request to the timer -- will go to sleep till done */
  76.   DoIO((struct IORequest *) ti->TimerIO );
  77. }
  78.  
  79.  
  80. void timer_delete(Timer_Info *ti)
  81. {
  82.   struct MsgPort *tp;
  83.  
  84.   if ( ! ti->created )
  85.     return;
  86.  
  87.  
  88.   if (ti->TimerIO != 0 )
  89.   {
  90.     tp = ti->TimerIO->tr_node.io_Message.mn_ReplyPort;
  91.  
  92.     if (tp != 0)
  93.         DeletePort(tp);
  94.  
  95.     CloseDevice( (struct IORequest *) ti->TimerIO );
  96.     DeleteExtIO( (struct IORequest *) ti->TimerIO );
  97.   }
  98.  
  99.   ti->created = 0;
  100.  
  101. }
  102.  
  103.